<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        layout="vertical">
        <a-form-model-item
          prop="password"
          :rules="validatorRules.oldpassword.rules"
          :maxLength="15"
          label="旧密码">
          <a-input
            type="password"
            placeholder="请输入旧密码"
            v-model="formInfo.password"/>
        </a-form-model-item>

        <a-form-model-item
          prop="password"
          :rules="validatorRules.password.rules"
          :maxLength="15"
          label="新密码">
          <a-input
            type="password"
            placeholder="请输入新密码"
            v-model="formInfo.repassword"/>
        </a-form-model-item>

        <a-form-model-item
          prop="repassword"
          :rules="validatorRules.confirmpassword.rules"
          :maxLength="15"
          label="确认新密码">
          <a-input
            type="repassword"
            placeholder="请输入新密码"
            v-model="formInfo.repassword2"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { postAction } from '@/api/manage'

  export default {
    name: 'UserPassword',
    data () {
      return {
        title: '修改密码',
        modalWidth: 600,
        visible: false,
        confirmLoading: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        validatorRules: {
          oldpassword: {
            rules: [{
              required: true, message: '请输入旧密码!'
            }]
          },
          password: {
            rules: [{
              required: true, message: '请输入新密码!'
            }]
          },
          confirmpassword: {
            rules: [{
              required: true, message: '请确认新密码!'
            }]
          }
        },
        url: 'system/company/updatePassword',
        formInfo: {
          password: '',
          repassword: '',
          repassword2: ''
        }
      }
    },
    methods: {
      show () {
        this.visible = true
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$refs.ruleForm.resetFields()
        this.visible = false
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            if (this.formInfo.repassword === this.formInfo.repassword2) {
              postAction(this.url, this.formInfo).then(res => {
                this.$refs.ruleForm.resetFields()
                this.$message.success(res.content)
                that.close()
              }).finally(() => {
                that.confirmLoading = false
              })
            } else {
              this.$message.error('两次密码不一致')
              this.confirmLoading = false
            }
          } else {
            this.confirmLoading = false
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
